Use runit instead of upstart as init system

Dominik Sander 8 年 前
コミット
cec02c1bd6
共有3 個のファイルを変更した122 個の追加34 個の削除を含む
  1. 11 20
      doc/manual/installation.md
  2. 17 14
      doc/manual/update.md
  3. 94 0
      lib/tasks/production.rake

+ 11 - 20
doc/manual/installation.md

@@ -48,7 +48,7 @@ Import node.js repository (can be skipped on Ubuntu and Debian Jessie):
48 48
 
49 49
 Install the required packages (needed to compile Ruby and native extensions to Ruby gems):
50 50
 
51
-    sudo apt-get install -y build-essential git zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake nodejs graphviz
51
+    sudo apt-get install -y runit build-essential git zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake nodejs graphviz
52 52
 
53 53
 
54 54
 ## 2. Ruby
@@ -229,6 +229,10 @@ When done you see `See the Huginn Wiki for more Agent examples!  https://github.
229 229
 
230 230
     sudo -u huginn -H bundle exec rake db:seed RAILS_ENV=production SEED_USERNAME=admin SEED_PASSWORD=yourpassword
231 231
 
232
+### Compile Assets
233
+
234
+    sudo -u huginn -H bundle exec rake assets:precompile RAILS_ENV=production
235
+
232 236
 ### Install Init Script
233 237
 
234 238
 Huginn uses foreman to generate the init scripts based on a `Procfile`
@@ -237,31 +241,20 @@ Edit the `Procfile` and choose one of the suggested versions for production
237 241
 
238 242
     sudo -u huginn -H editor Procfile
239 243
 
240
-**Debian only** Install upstart and reboot the system (skip this step on Ubuntu):
241
-
242
-    sudo apt-get install -y --force-yes upstart
243
-    sudo reboot
244
-    # After you you logged back in go to Huginn installation folder
245
-    cd /home/huginn/huginn
246
-
247
-Export the init scripts using foreman:
244
+Export the init scripts:
248 245
 
249
-    sudo rm /etc/init/huginn*
250
-    sudo foreman export upstart -a huginn /etc/init
246
+    sudo rake production:export
251 247
 
252
-**Note:** You have to re-export the init script every time you change the configuration in `.env`!
248
+**Note:** You have to re-export the init script every time you change the configuration in `.env` or your `Procfile`!
253 249
 
254 250
 ### Setup Logrotate
255 251
 
256 252
     sudo cp lib/support/logrotate/huginn /etc/logrotate.d/huginn
257 253
 
258
-### Compile Assets
259
-
260
-    sudo -u huginn -H bundle exec rake assets:precompile RAILS_ENV=production
261 254
 
262
-### Start Your Huginn Instance
255
+### Ensure Your Huginn Instance Is Running
263 256
 
264
-    sudo start huginn
257
+    sudo rake production:status
265 258
 
266 259
 ## 7. Nginx
267 260
 
@@ -335,9 +328,7 @@ Restart Nginx, export the init script and restart Huginn:
335 328
 ```
336 329
 cd /home/huginn/huginn
337 330
 sudo service nginx restart
338
-sudo rm /etc/init/huginn*
339
-sudo foreman export upstart -a huginn /etc/init
340
-sudo restart huginn
331
+sudo rake production:export
341 332
 ```
342 333
 
343 334
 Using a self-signed certificate is discouraged, but if you must use it follow the normal directions. Then generate the certificate:

+ 17 - 14
doc/manual/update.md

@@ -1,19 +1,25 @@
1 1
 # Update
2 2
 
3
-### 0. Stop server
3
+### 0. Ensure depencies are up to date
4 4
 
5 5
 ```
6
-sudo stop huginn
6
+cd /home/huginn/huginn
7
+sudo rake production:check
7 8
 ```
8 9
 
9
-### 1. Store the current version
10
+### 1. Stop server
11
+
12
+```
13
+sudo rake production:stop
14
+```
15
+
16
+### 2. Store the current version
10 17
 
11 18
 ```
12
-cd /home/huginn/huginn
13 19
 export OLD_VERSION=`git rev-parse HEAD`
14 20
 ```
15 21
 
16
-### 2. Update the code
22
+### 3. Update the code
17 23
 
18 24
 Back up changed files
19 25
 
@@ -35,7 +41,7 @@ Restore backed up files
35 41
 sudo -u huginn -H cp Procfile.bak Procfile
36 42
 ```
37 43
 
38
-### 3. Install gems, migrate and precompile assets
44
+### 4. Install gems, migrate and precompile assets
39 45
 
40 46
 ```
41 47
 cd /home/huginn/huginn
@@ -46,11 +52,11 @@ sudo -u huginn -H bundle install --deployment --without development test
46 52
 sudo -u huginn -H bundle exec rake db:migrate RAILS_ENV=production
47 53
 
48 54
 # Clean up assets and cache
49
-sudo -u huginn -H bundle exec rake assets:clean assets:precompile cache:clear RAILS_ENV=production
55
+sudo -u huginn -H bundle exec rake assets:clean assets:precompile tmp:cache:clear RAILS_ENV=production
50 56
 
51 57
 ```
52 58
 
53
-### 4. Update the Procfile
59
+### 5. Update the Procfile
54 60
 
55 61
 Check for changes made to the default `Procfile`
56 62
 ```
@@ -62,7 +68,7 @@ Update your `Procfile` if the default options of the version you are using chang
62 68
 sudo -u huginn -H editor Procfile
63 69
 ```
64 70
 
65
-### 5. Update the .env file
71
+### 6. Update the .env file
66 72
 
67 73
 Check for changes made to the example `.env`
68 74
 ```
@@ -75,13 +81,10 @@ sudo -u huginn -H editor .env
75 81
 ```
76 82
 
77 83
 
78
-### 6. Export init script and start Huginn
84
+### 7. Export init script and start Huginn
79 85
 
80 86
 ```
81 87
 # Export the init script
82
-sudo rm /etc/init/huginn*
83
-sudo foreman export upstart -a huginn /etc/init
84
-# Start Huginn
85
-sudo start huginn
88
+sudo rake production:export
86 89
 ```
87 90
 

+ 94 - 0
lib/tasks/production.rake

@@ -0,0 +1,94 @@
1
+def failed; "[ \033[31mFAIL\033[0m ]"; end
2
+def ok;     "[  \033[32mOK\033[0m  ]"; end
3
+
4
+def run_as_root
5
+  return true if ENV['USER'] == 'root'
6
+  puts "#{failed} Please run this command as root or with sudo\n\n"
7
+  exit -1
8
+end
9
+
10
+def runit_installed
11
+  return true unless `which sv` && $?.to_i != 0
12
+  puts "#{failed} Please install runit: \n\nsudo apt-get install runit\n\n"
13
+  exit -1
14
+end
15
+
16
+def remove_upstart_config
17
+  return true unless File.exists?('/etc/init/huginn.conf')
18
+  puts "#{failed} Please stop huginn and remove the huginn upstart init scripts:\n\n"
19
+  puts "sudo stop huginn"
20
+  puts "sudo rm /etc/init/huginn*\n\n"
21
+  exit -1
22
+end
23
+
24
+namespace :production do
25
+  task :check do |t|
26
+    remove_upstart_config
27
+    runit_installed
28
+    puts "#{ok} Everything is fine" if t.application.top_level_tasks.include? 'production:check'
29
+  end
30
+
31
+  task :stop => :check do
32
+    puts "Stopping huginn ..."
33
+    run_sv('stop')
34
+  end
35
+
36
+  task :start => :check do
37
+    puts "Startig huginn ..."
38
+    run_sv('start')
39
+  end
40
+
41
+  task :status => :check do
42
+    run_sv('status')
43
+  end
44
+
45
+  task :restart => :check do
46
+    puts "Restarting huginn ..."
47
+    run_sv('restart')
48
+  end
49
+
50
+  task :export => :check do
51
+    run_as_root
52
+    Rake::Task['production:stop'].execute
53
+    puts "Exporting new services ..."
54
+    run('rm -rf /etc/service/huginn*')
55
+    run('foreman export runit -a huginn -l /home/huginn/huginn/log /etc/service')
56
+    services = Dir.glob('/etc/service/huginn*')
57
+    while services.length > 0
58
+      services.each do |p|
59
+        supervise = File.join(p, 'supervise')
60
+        next if !Dir.exists?(supervise)
61
+        run("chown -R huginn:huginn #{p}")
62
+        services.delete(p)
63
+      end
64
+      sleep 0.1
65
+    end
66
+  end
67
+end
68
+
69
+def run_sv(command)
70
+  Dir.glob('/etc/service/huginn*').each do |p|
71
+    with_retries do
72
+      run("sv #{command} #{File.basename(p)}")
73
+    end
74
+  end
75
+end
76
+
77
+def run(cmd, verbose=false)
78
+  output = `#{cmd}`
79
+  if $?.to_i != 0
80
+    raise "'#{cmd}' exited with a non-zero return value: #{output}"
81
+  end
82
+  puts output if verbose && output.strip != ''
83
+  output
84
+end
85
+
86
+def with_retries(&block)
87
+  tries ||= 5
88
+  output = block.call
89
+rescue StandardError => e
90
+  retry unless (tries -= 1).zero?
91
+  raise e
92
+else
93
+  puts output
94
+end